package com.weather.ui;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.IBinder;

import java.io.File;
import java.io.IOException;

public class PlayService extends Service{
    private MediaPlayer player;
    public PlayService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        player = new MediaPlayer();
//        play.setOnCompletionListener(MainActivity.this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        boolean play = intent.getBooleanExtra("play", true);
        String path = intent.getStringExtra("path");
        if (!play){
            try {
                //重置
                player.reset();
                //设置要播放的多媒体文件的路径
                player.setDataSource(this, Uri.fromFile(new File(path)));
                //是否循环播放
                player.setLooping(false);
                //准备播放
                player.prepare();
                //开始播放
                player.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw null;
    }
}
